package com.inifitness.plugin.client;

import com.inifitness.plugin.service.QyxxClient;

import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;

/**
 * TODO: (Mageric 2024-04-21)
 *
 * @author sunjinfeng
 * @version 1.0.0
 * @since 2024-04-21 22:58
 */
@Configuration
public class WebConfig {

  @Value("${aliyun.config.appcode:6e8802956cbe49e7b1113faf83b54dbc}")
  private String appcode;


  @Bean
  WebClient webClient() {
    return WebClient.builder()
        .baseUrl("https://jsonplaceholder.typicode.com/")
        .defaultHeader("Authorization", "APPCODE " + appcode)
        .build();
  }

  @SneakyThrows
  @Bean
  QyxxClient postClient(WebClient webClient) {
    HttpServiceProxyFactory httpServiceProxyFactory =
        HttpServiceProxyFactory.builderFor(WebClientAdapter.create(webClient))
            .build();
    return httpServiceProxyFactory.createClient(QyxxClient.class);
  }
}
